package com.kycxika.app.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.kycxika.app.R;
import com.kycxika.app.bean.downBean;
import com.kycxika.app.utils.APPUtil;
import com.kycxika.app.view.CircleImageView;
import com.kycxika.app.view.ProgressButton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/20.
 */

public class StartSiveRecyAdapter extends RecyclerView.Adapter<StartSiveRecyAdapter.ViewHolder> {
    private Context mContext;
    private List<downBean> mList = new ArrayList<>();
    private onSelectorItem onSelector;
    private Boolean isPause = false;
    private ViewHolder mHodler;

    public StartSiveRecyAdapter(Context context) {
        this.mContext = context;
    }

    public void setList(List<downBean> list) {
        mList.clear();
        mList.addAll(list);
        this.notifyDataSetChanged();
    }

    public Handler handler = new Handler() {

        private ProgressButton button;

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    int fraction = msg.arg1;
                    int pos = msg.arg2;
//                    if ((Integer) mHodler.btnDown.getTag() == pos) {
                    mHodler.btnDown.setProgress(fraction);
                        mHodler.progress.setProgress(fraction);
                   // }
                    break;
                case 101:
                    int posi = msg.arg2;
                   // if ((Integer) mHodler.btnDown.getTag() == posi) {
                        mHodler.btnDown.setProgress(100);
                        mHodler.btnDown.setText("完成");
                   // }
                    break;
            }
        }
    };


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.star_list_item, parent, false));
        return viewHolder;
    }

    /*public boolean bool = false;

    private boolean mIsSelectable = false;

    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    //根据位置判断条目是否可选
    private boolean isSelectable() {
        return mIsSelectable;
    }

    //设置给定位置条目的可选与否的状态
    private void setSelectable(boolean selectable) {
        mIsSelectable = selectable;
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //获得选中条目的结果
    public ArrayList<String> getSelectedItem() {
        ArrayList<String> selectList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(mList.get(i).getName() + "," + mList.get(i).getSex());
            }
        }
        return selectList;
    }*/

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        mHodler = holder;
        final downBean downBean = mList.get(position);
        Glide.with(mContext)
                .load(downBean.getImageUrl())
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                .error(R.drawable.ic_launcher)
                .into(new GlideDrawableImageViewTarget(holder.image) {
                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                    }

                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                        super.onResourceReady(resource, animation);
                    }
                });

        holder.name.setText(downBean.getName());
        holder.version.setText(downBean.getNewVersionCode());
        holder.version.setVisibility(View.VISIBLE);
        holder.progress.setVisibility(View.GONE);
        holder.progress.setProgress(0);
        holder.progress.setMax(100);
        holder.btnDown.setText("下载");
        holder.btnDown.initState();

        holder.btnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //holder.btnDown.setTag(position);
                holder.progress.setVisibility(View.VISIBLE);
                holder.version.setVisibility(View.GONE);
                //holder.progress.setProgress(0);
                //if((Integer)mHolder.btnDown.getTag() == position) {
                APPUtil.removeOldApk();
                holder.btnDown.isShowProgressNum(true);
                //downApk(downBean.getDownUrl(),downBean.getName());
                if (onSelector != null) {
                    onSelector.onClickSeleterItem(downBean, position);
                }
                //mHolder.btnDown.setTag(position+1);
                if (!holder.btnDown.isFinish()) {
                    holder.btnDown.toggle();
                }
                // }

                //mHolder.btnDown.setProgress(MainActivity.instance.getProgressDown());
            }
        });
        holder.btnDown.setOnStateListener(new ProgressButton.OnStateListener() {
            @Override
            public void onFinish() {
                isPause = true;
                // mHolder.btnDown.setText("完成");
            }

            @Override
            public void onStop() {
                isPause = true;
                holder.btnDown.setText("继续");
            }

            @Override
            public void onContinue() {
                isPause = false;
                //mHolder.btnDown.setText("暂停");
            }
        });


    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView image;
        TextView name;
        TextView version;
        ProgressButton btnDown;
        ProgressBar progress;

        public ViewHolder(View itemView) {
            super(itemView);

            name = (TextView) itemView.findViewById(R.id.star_item_name);
            image = (CircleImageView) itemView.findViewById(R.id.star_item_image);
            version = (TextView) itemView.findViewById(R.id.star_item_version);
            btnDown = (ProgressButton) itemView.findViewById(R.id.star_item_btn);
            progress = (ProgressBar) itemView.findViewById(R.id.progress);

        }
    }

    public void setOnSelect(onSelectorItem onSelectorItem) {
        this.onSelector = onSelectorItem;
    }

    public interface onSelectorItem {

        void onClickSeleterItem(downBean path, int position);

    }

}
